String  inputString = "";        
boolean stringComplete = false; 
 
char  startString=0x24;
char  endString=0x23;

long vol;
long current;
long fact;
long power;

#define MSG_DATA_LENTH 10

#define MSG_V 0x01
#define MSG_I 0x02
#define MSG_FACT 0x09
#define MSG_P 0x0b
//查询控制字
byte SendMessage[10]={0x24,0xff,0x03,0xaa,0x00,0x00,0x00,0x00,0xac,0x23};
//表示数据获取情况
byte MsgGetNumber=0x00;
boolean SendMSGFlag=false;

typedef struct
{
  int i;
  int v;
  long wp;
  long rp;
  int powfact;
  int freq;
  int cumpower;
} ElecData;

typedef struct
{
	byte address;
	byte funcode;
	byte paraddress;
	byte data5;
	byte data6;
	byte data7;
	byte data8;
	byte crc;
}JunLinData;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 100 bytes for the inputString:
  inputString.reserve(100);
}
//表示解析出多少个数据
int parseIndex=0;

void loop() {
// print the string when a newline arrives:
	if (stringComplete) {
    //Serial.println(inputString);
    if(inputString[0]==startString && inputString.length()==MSG_DATA_LENTH)	
	{
	    //Serial.println("begin prase");
		JunLinData tmpData=PraseData();
		//boolean crcR=CRCSum(tmpData);
		//if(crcR)
		//{
			switch(tmpData.paraddress)
			{
				case MSG_V:
					vol=Change2Real(tmpData);
						Serial.println(vol);
	
					bitSet(MsgGetNumber,1);
					break;
				case MSG_I:
					current=Change2Real(tmpData);
					Serial.println(current);

					bitSet(MsgGetNumber,2);					
					break;
				case MSG_FACT:
					fact=Change2Real(tmpData);
					bitSet(MsgGetNumber,3);
					Serial.println(fact);
	
					break;
				case MSG_P:
					power=Change2Real(tmpData);
					bitSet(MsgGetNumber,4);
					Serial.println(power);
					break;
			}			
		//}
		//PrintJunLinData(tmpData);
	}
    // clear the string:
    inputString = "";
    stringComplete = false;
	}
}
/*
void QueryJunLin()
{	
	//将数据发出
	if(SendMSGFlag==false)
	{
		Serial.write(SendMessage,10);
		SendMSGFlag=true;
		MsgGetNumber=0x00;
	}
	
}
*/

long Change2Real(JunLinData tmpData)
{
    //Serial.println(tmpData.data8);
	byte gw=byte(tmpData.data8&0x0F);
    //Serial.println(gw,HEX);
	byte sw=byte((tmpData.data8>>4)&0x0F);
	//Serial.println(sw,HEX);
	
	byte bw=byte(tmpData.data7&0x0F);
	
	byte qw=byte((tmpData.data7>>4)&0x0F);
	
	byte ww=byte(tmpData.data6&0x0F);
	byte sww=byte((tmpData.data6>>4)&0x0F);
	
	long sum=long(gw)+long(sw)*10+long(bw)*100+long(qw)*1000+long(ww)*10000+long(sww)*100000;
	
	return sum;
}

JunLinData PraseData()
{
	JunLinData data;
	data.address=byte(inputString[1]);
	data.funcode=byte(inputString[2]);
	data.paraddress=byte(inputString[3]);
	data.data5=byte(inputString[4]);
	data.data6=byte(inputString[5]);
	data.data7=byte(inputString[6]);
	data.data8=byte(inputString[7]);
	data.crc=byte(inputString[8]);
	return data;
}

boolean CRCSum(JunLinData data)
{
  boolean result=false;
  byte crcSum=0x00;
  crcSum=crcSum+data.address+data.funcode;
  crcSum=crcSum+data.paraddress+data.data5;
  crcSum=crcSum+data.data6+data.data7+data.data8;
  if (crcSum==data.crc)
	result=true;
  return result;
}

void PrintJunLinData(JunLinData data)
{
	Serial.println(data.address,HEX);
	Serial.println(data.funcode,HEX);
	Serial.println(data.paraddress,HEX);
	Serial.println(data.data5,HEX);
	Serial.println(data.data6,HEX);
	Serial.println(data.data7,HEX);
	Serial.println(data.data8,HEX);
	Serial.println(data.crc,HEX);
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();	
	inputString += inChar;
    // check end code
    if (inChar == endString) {
      stringComplete = true;
    }
  }
}

